package erp

import (
	"context"
	"encoding/json"
	"ez/apps/erp/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type Seller struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64           `json:"id" bson:"id,omitempty"`
	Name        string          `json:"name" bson:"name"`         //名称
	FastName    string          `json:"fastName" bson:"fastName"` //助记名称
	Cat         *SellerCategory `json:"cat" bson:"cat"`           //供应商分类
	CatId       int64           `json:"catId" bson:"catId"`       //供应商分类
	Level       *SellerLevel    `json:"level" bson:"level"`       //供应商等级
	LevelId     int64           `json:"levelId" bson:"levelId"`   //供应商等级
	IsOn        bool            `json:"isOn" bson:"isOn"`         //是否有效
	CreateAt    time.Time       `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time       `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time      `json:"deleteAt" bson:"deleteAt"`
}

func (this *Seller) DocName() string { return "Seller" }
func (this *Seller) GetId() int64    { return this.Id }
func (this *Seller) SetId(id int64)  { this.Id = id }
func (this *Seller) Create() error {
	return this.GetFactory().Create(this)
}
func (this *Seller) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *Seller) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *Seller) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *Seller) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *Seller) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *Seller) ToString() string {
	return string(this.ToBytes())
}
func (this *Seller) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Seller) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *Seller) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *Seller) LoadCat() {
	if this.CatId == 0 {
		return
	}
	this.Cat, _ = NewSellerCategoryCrud().FindId(this.CatId)
}
func (this *Seller) LoadLevel() {
	if this.LevelId == 0 {
		return
	}
	this.Level, _ = NewSellerLevelCrud().FindId(this.LevelId)
}
func (this *Seller) ClearRelationsBeforeSave() mgo.Doc {
	this.Cat = nil
	this.Level = nil
	return this
}
func neverUsed_Seller() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type SellerAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *Seller
	Session *ez.Session
}

func NewSellerAccessControl(model *Seller, action string, session *ez.Session) *SellerAccessControl {
	ctrl := &SellerAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.SellerAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
